//
//  WBComposeToolBar.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/13.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit


enum WBComposeToolBarType:Int {
    case pictue = 0
    case mention = 1
    case trend = 2
    case emoticon = 3
    case add = 4
}

class WBComposeToolBar: UIStackView {

    var callBack:((WBComposeToolBarType)->())?
   
    var emoticonButton:UIButton!
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 设置水平方向布局
        self.axis = .horizontal
        // 设置子视图填充方式
        self.distribution = .fillEqually
        addChilButton(imageName: "compose_toolbar_picture", type: .pictue)
        addChilButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChilButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChilButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChilButton(imageName: "compose_add_background", type: .add)

        
    }
    @discardableResult // 忽略返回值
    private func addChilButton(imageName:String,type:WBComposeToolBarType) ->UIButton   {
        
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(clickButton(btn:)), for: .touchUpInside)
        button.setImage(UIImage(named:imageName), for: .normal)
        button.setImage(UIImage(named:imageName + "_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: .normal)
        button.adjustsImageWhenDisabled = false
        // 添加子视图
        addArrangedSubview(button)
        
        return button
    }
    
    @objc private func clickButton(btn:UIButton) {
        
        let type = WBComposeToolBarType(rawValue:btn.tag)!
        
        callBack?(type)
    }
    
    func switchButtonIcon(isEmoticonKeyBoard:Bool) {
        
        if isEmoticonKeyBoard {
            
            emoticonButton.setImage(UIImage(named:"compose_keyboardbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named:"compose_keyboardbutton_background_highlighted"), for: .highlighted)

        } else {
            
            emoticonButton.setImage(UIImage(named:"compose_emoticonbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named:"compose_emoticonbutton_background_highlighted"), for: .highlighted)

        }
    }
}
